#include<stdio.h>
#include<stdlib.h>


typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;

//树节点的定义
struct TNode
{
    ElementType Data;//节点数据
    BinTree Left;//指向左子树
    BinTree Right;//指向右子树
};

BinTree build_tree(int *Inorder,int *Preorder,int n);
int get_height(BinTree T);
void LevelorderTraversal(BinTree T,int n);

int main()
{
    int n;
    scanf("%d\n",&n);
    int *Inorder=(int*)malloc(sizeof(int)*n*2);
    for(int i=0;i<n;i++)
        scanf("%d",&Inorder[i]);
    int *Preorder=(int*)malloc(sizeof(int)*n*2);
    for(int i=0;i<n;i++)
        scanf("%d",&Preorder[i]);
    BinTree T=build_tree(Inorder,Preorder,n);
    LevelorderTraversal(T,n);
    return 0;

}

BinTree build_tree(int *Inorder,int *Preorder,int n)
{
    BinTree T;
    int i=0;
    T=(BinTree)malloc(sizeof(struct TNode));
    T->Data=Preorder[0];
    T->Left=T->Right=NULL;
    for( i=1;i<=n;i++)
    { 
        if(Inorder[i-1]==Preorder[0]) break;
    }
       
    if(n-i!=0)
    {
        T->Right=build_tree(Inorder+i-1,Preorder+i-1,n-i);
    }
    if(i-1!=0)
    {
        T->Left=build_tree(Inorder,Preorder+1,i-1);
    }


    return T;
}

void LevelorderTraversal(BinTree T,int n){
    BinTree *Q=(BinTree*)malloc(sizeof(BinTree)*(n*2));
    int now=0,i=0;
    for(;now<n;){
        if(Q[now]->Right!=NULL)
        {
            Q[++i]=Q[now]->Right;
        }
        if(Q[now]->Left!=NULL)
        {
            Q[++i]=Q[now]->Left;
        }
        now++;
    }
    for(int i=0;i<n;i++)
    {
        if(i!=0)
            printf(" %d",Q[i]->Data);
        else
            printf("%d",Q[i]->Data);
    }
}


